package cn.t.repository.impl.user;

import cn.t.converter.user.CustomerInfoConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.CustomerInfoDO;
import cn.t.persistence.entity.user.CustomerInfoPO;
import cn.t.persistence.mapper.user.CustomerInfoMapper;
import cn.t.repository.user.CustomerInfoRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户信息表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CustomerInfoRepositoryImpl extends RepositoryImpl<CustomerInfoMapper, CustomerInfoPO, CustomerInfoConverter, CustomerInfoDO> implements CustomerInfoRepository {


    @Override
    public List<CustomerInfoDO> findByModel(CustomerInfoDO customerInfoDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<CustomerInfoPO> list = getWrapper(customerInfoDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<CustomerInfoPO> getWrapper(CustomerInfoDO customerInfoDO) {
        LambdaQueryChainWrapper<CustomerInfoPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(customerInfoDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(customerInfoDO.getGmtCreatedEnd())) {
            wrapper.between(CustomerInfoPO::getGmtCreated, customerInfoDO.getGmtCreatedStart(), customerInfoDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(customerInfoDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(customerInfoDO.getGmtModifiedEnd())) {
            wrapper.between(CustomerInfoPO::getGmtModified, customerInfoDO.getGmtModifiedStart(), customerInfoDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(customerInfoDO.getCustomerType())) {
            wrapper.eq(CustomerInfoPO::getCustomerType, customerInfoDO.getCustomerType());
        }
        if (!ObjectUtils.isEmpty(customerInfoDO.getCustomerCode())) {
            wrapper.eq(CustomerInfoPO::getCustomerCode, customerInfoDO.getCustomerCode());
        }
        if (!ObjectUtils.isEmpty(customerInfoDO.getCertificateStatus())) {
            wrapper.eq(CustomerInfoPO::getCertificateStatus, customerInfoDO.getCertificateStatus());
        }
        if (!ObjectUtils.isEmpty(customerInfoDO.getStatus())) {
            wrapper.eq(CustomerInfoPO::getStatus, customerInfoDO.getStatus());
        }
        return wrapper;
    }

    @Override
    public long count(CustomerInfoDO customerInfoDO) {
        return getWrapper(customerInfoDO).count();
    }

    @Override
    public boolean deleteCustomer(Long customerId) {
        return lambdaUpdate().set(CustomerInfoPO::getStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(CustomerInfoPO::getIsDeleted, String.valueOf(YesNoEnum.TRUE.getCode()))
                .set(CustomerInfoPO::getGmtModified, LocalDateTime.now()).eq(CustomerInfoPO::getId, customerId).update();

    }

    @Override
    public void disableIndividualCustomer(Long customerId) {
        lambdaUpdate().set(CustomerInfoPO::getStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(CustomerInfoPO::getIsDeleted, String.valueOf(YesNoEnum.FALSE.getCode()))
                .set(CustomerInfoPO::getGmtModified, LocalDateTime.now()).eq(CustomerInfoPO::getId, customerId).update();
    }

    @Override
    public void enableIndividualCustomer(Long customerId) {
        lambdaUpdate().set(CustomerInfoPO::getStatus, String.valueOf(BaseStatusEnum.AVAILABLE.getCode()))
                .set(CustomerInfoPO::getIsDeleted, String.valueOf(YesNoEnum.FALSE.getCode()))
                .set(CustomerInfoPO::getGmtModified, LocalDateTime.now()).eq(CustomerInfoPO::getId, customerId).update();
    }
}
